﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Controls.Primitives;

namespace TaxLawView
{
    public class LazyTreeViewItem : TreeViewItem
    {
        private Panel _loadingPanel;
        private Panel _itemsContainer;
        private Storyboard _loadingStoryBoard;
        private bool _isLoading;
        private bool IsLoading
        {
            get { return _isLoading; }
            set
            {
                _isLoading = value;
                EnsureLoadingState(value);
            }
        }

        private void EnsureLoadingState(bool value)
        {
            if (_loadingPanel == null || _itemsContainer == null)
                return;
            if (value)
            {
                _loadingPanel.Visibility = IsExpanded ? Visibility.Visible : Visibility.Collapsed;
                _itemsContainer.Visibility = Visibility.Collapsed;
            }
            else
            {
                _loadingPanel.Visibility = Visibility.Collapsed;
                _itemsContainer.Visibility = Visibility.Visible;
            }
        }

        static LazyTreeViewItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(LazyTreeViewItem), new FrameworkPropertyMetadata(typeof(LazyTreeViewItem)));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _loadingPanel = GetTemplateChild("LoadingPanel") as Panel;
            _itemsContainer = GetTemplateChild("ItemsContainer") as Panel;
            _loadingStoryBoard = GetTemplateChild("loadingStoryBoard") as Storyboard;
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new LazyTreeViewItem();
        }
        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            base.PrepareContainerForItemOverride(element, item);
        }
        protected override void OnExpanded(RoutedEventArgs e)
        {
            base.OnExpanded(e);
            NotifyItemExpanded(this);
            EnsureLoadingState(IsLoading);
            if (IsLoading)
                return;
            ILazy lazy = this.DataContext as ILazy;
            if (lazy != null && !lazy.IsLoaded)
            {
                this.IsLoading = true;
                if (_loadingStoryBoard != null)
                    _loadingStoryBoard.Begin();
                lazy.Load(() =>
                {
                    this.IsLoading = false;
                    BindingExpression exp = this.GetBindingExpression(ItemsSourceProperty);
                    if (exp != null && exp.ParentBinding != null)
                    {
                        this.SetBinding(ItemsSourceProperty, exp.ParentBinding);
                    }
                });
            }
        }
        protected override void OnCollapsed(RoutedEventArgs e)
        {
            base.OnCollapsed(e);
            EnsureLoadingState(IsLoading);
        }
        private void NotifyItemExpanded(LazyTreeViewItem item)
        {
            if (item == null)
                return;
            LazyTreeViewItem parentItem = item.Parent as LazyTreeViewItem;
            if (parentItem != null)
            {
                parentItem.NotifyItemExpanded(item);
            }
            else
            {
                LazyTreeView parentView = Parent as LazyTreeView;
                if (parentView != null)
                {
                    parentView.OnItemExpanded(item);
                }
            }
        }
    }
}
